package controller.taskManagement;

import reference.codes.CodeTask;
import utilities.clock.Clock;
import utilities.clock.ClockListener;
import controller.algorithms.AlgorithmManager;
import controller.environment.ground.Ground;
import controller.environment.ground.GroundFlowered;
import controller.environment.ground.GroundSeeded;

public class Task implements ClockListener {
	private Ground m_ground;
	private boolean m_inProgress;
	
	public Task(Ground ground){
		m_ground = ground;
		m_inProgress = false;
		Clock.getClock().addClockListener(this);
	}

	public int getPosX() {
		return m_ground.getPosX();
	}

	public int getPosY() {
		return m_ground.getPosY();
	}

	public double getQuantityOfWork() {
		return m_ground.getAmountWork();
	}

	public void setQuantityOfWork(double quantity_of_work) {
		m_ground.setAmountWork(quantity_of_work);
	}

	public int getTypeOfTask() {
		if(m_ground instanceof GroundSeeded){
			return CodeTask.TASK_SEEDED;
		}else if(m_ground instanceof GroundFlowered){
			return CodeTask.TASK_FLOWERED;
		}
		return 0;
	}

	public void acts() {
		AlgorithmManager.getInstance().getAlgorithm().nextStepTask(this);
		if(m_ground==null || m_ground.getAmountWork()<=0.0){
			TaskManagement.getTaskManagement().remove(this);
		}
	}

	public void setClock(Clock this_clock) {
	}
	
	/**
	 * Performs the work
	 * @param quantity_of_work the quantity of work performed
	 * @return true if the task is not finished yet.
	 */
	public boolean perform(double quantity_of_work){
		return m_ground.diminishWork(quantity_of_work);
	}
	
	public boolean isGround(Ground ground){
		if(ground==null)
			return false;
		return ground.equals(m_ground);
	}

	public boolean isInProgress() {
		return m_inProgress;
	}

	public void setInProgress(boolean in_progress) {
		this.m_inProgress = in_progress;
	}
}
